<?php
/**
 * 栖岛登录小工具类
 */
class Qidao_Connect_Widget extends WP_Widget {
    /**
     * 构造函数
     */
    public function __construct() {
        parent::__construct(
            'qidao_connect_widget',
            __('栖岛账号登录', 'qidao-connect'),
            array(
                'description' => __('显示栖岛账号登录按钮', 'qidao-connect'),
                'classname' => 'qidao-connect-widget',
            )
        );
    }
    
    /**
     * 前端显示
     */
    public function widget($args, $instance) {
        // 如果用户已登录且不显示给已登录用户，则不显示小工具
        if (is_user_logged_in() && !empty($instance['hide_for_logged_in'])) {
            return;
        }
        
        $title = !empty($instance['title']) ? $instance['title'] : __('使用栖岛账号登录', 'qidao-connect');
        $title = apply_filters('widget_title', $title, $instance, $this->id_base);
        $show_text = !empty($instance['show_text']) ? true : false;
        
        echo $args['before_widget'];
        
        if ($title) {
            echo $args['before_title'] . $title . $args['after_title'];
        }
        
        echo '<div class="qidao-connect-widget-content">';
        
        if (is_user_logged_in()) {
            $current_user = wp_get_current_user();
            echo '<p>' . sprintf(__('已登录为 %s', 'qidao-connect'), $current_user->display_name) . '</p>';
            echo '<p><a href="' . wp_logout_url(home_url()) . '">' . __('退出登录', 'qidao-connect') . '</a></p>';
        } else {
            // 获取主类实例
            global $qidao_connect;
            if (!$qidao_connect) {
                $qidao_connect = new Qidao_Connect();
            }
            
            echo $qidao_connect->login_button($show_text);
        }
        
        echo '</div>';
        
        echo $args['after_widget'];
    }
    
    /**
     * 后台表单
     */
    public function form($instance) {
        $title = !empty($instance['title']) ? $instance['title'] : __('使用栖岛账号登录', 'qidao-connect');
        $show_text = !empty($instance['show_text']) ? true : false;
        $hide_for_logged_in = !empty($instance['hide_for_logged_in']) ? true : false;
        ?>
        <p>
            <label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('标题:', 'qidao-connect'); ?></label>
            <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo esc_attr($title); ?>">
        </p>
        <p>
            <input class="checkbox" type="checkbox" <?php checked($show_text); ?> id="<?php echo $this->get_field_id('show_text'); ?>" name="<?php echo $this->get_field_name('show_text'); ?>">
            <label for="<?php echo $this->get_field_id('show_text'); ?>"><?php _e('显示文本链接而不是图标', 'qidao-connect'); ?></label>
        </p>
        <p>
            <input class="checkbox" type="checkbox" <?php checked($hide_for_logged_in); ?> id="<?php echo $this->get_field_id('hide_for_logged_in'); ?>" name="<?php echo $this->get_field_name('hide_for_logged_in'); ?>">
            <label for="<?php echo $this->get_field_id('hide_for_logged_in'); ?>"><?php _e('对已登录用户隐藏', 'qidao-connect'); ?></label>
        </p>
        <?php
    }
    
    /**
     * 更新小工具
     */
    public function update($new_instance, $old_instance) {
        $instance = array();
        $instance['title'] = (!empty($new_instance['title'])) ? sanitize_text_field($new_instance['title']) : '';
        $instance['show_text'] = (!empty($new_instance['show_text'])) ? 1 : 0;
        $instance['hide_for_logged_in'] = (!empty($new_instance['hide_for_logged_in'])) ? 1 : 0;
        
        return $instance;
    }
} 